﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BAL.Controller;
using EntityObjects;

namespace PresentationLayer.View.StoreClerk
{
    public partial class UpdateDisbursementList : System.Web.UI.Page
    {
        ViewDisbursementListForStoreControl control = new ViewDisbursementListForStoreControl();
        string disStatus;

        protected void Page_Load(object sender, EventArgs e)
        {
            Label3.Visible = false;
            if (IsPostBack == false)
            {
                List<Department> deptList = new List<Department>();
                deptList = control.getDisbursedDepartment();
                if (deptList != null)
                {
                    ddDepartment.DataSource = deptList;
                    ddDepartment.DataTextField = "deptName";
                    ddDepartment.DataValueField = "deptId";
                    ddDepartment.DataBind();
                }
                Bind();
                Label3.Text = "Is Not Post Back";
            }
            else
            {
                Label3.Text = "Is Post Back";
            }                        
        }
        private void Bind()
        {
            List<DisbursementCatalogue> data = control.getDisbursementItemToUpdate();
            gvDisbursementList.DataSource = data;
            gvDisbursementList.DataBind();
            ViewState["gvDisbursementList"] = data;
        }
        
        protected void ddDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            string deptId = ddDepartment.SelectedItem.Value;
            if (deptId == "0")
            {
                gvDisbursementList.DataSource = control.getDisbursementItemToUpdate();
            }
            else
            {
                gvDisbursementList.DataSource = control.getDisbursementItemToUpdate(deptId);
            }
            gvDisbursementList.DataBind();
        }

       
        protected void gvDisbursementList_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            control = new ViewDisbursementListForStoreControl();
            GridViewRow row = gvDisbursementList.Rows[e.RowIndex];

            Control region2 = row.Cells[3].FindControl("DropDownList1");
            DropDownList dd = (DropDownList)region2;

            Control region1 = row.Cells[5].FindControl("hdnDisId");
            Label hd = (Label)region1;
            int disId = Convert.ToInt32(hd.Text);

            disStatus = dd.SelectedItem.Value;
            bool flag = control.updateDisbursementStatus(disStatus, disId);

            Bind();
            //Clear existing items
            if (ddDepartment.Items.Count > 0)
            {
                ddDepartment.Items.Clear();
            }
            List<Department> deptList = new List<Department>();
                deptList = control.getDisbursedDepartment();
                if (deptList != null)
                {
                    ddDepartment.Items.Insert(0, new ListItem("ALL", "0"));
                    ddDepartment.DataSource = deptList;
                    ddDepartment.DataTextField = "deptName";
                    ddDepartment.DataValueField = "deptId";
                    ddDepartment.DataBind();
                }
        }

       
        protected void gvDisbursementList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDisbursementList.PageIndex = e.NewPageIndex;
            gvDisbursementList.DataSource = ViewState["gvDisbursementList"];
            gvDisbursementList.DataBind();
        }
    }
}